package tr.edu.ege.seagent.hobnobontology.instance;

import java.text.MessageFormat;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.rdf.model.RDFNode;

/**
 * @author etmen
 *
 */
/**
 * @author etmen
 * 
 */
public class HobNobResultSetSolution {
	private RDFNode dbpedia = null;

	private RDFNode label = null;

	private RDFNode location = null;

	private Logger logger;

	private RDFNode member = null;

	private RDFNode name = null;

	private RDFNode notification = null;

	private RDFNode notificationX = null;

	private RDFNode population = null;

	private QuerySolution querySolution = null;

	private RDFNode surname = null;

	private RDFNode type = null;

	private RDFNode xCoordinate = null;

	private RDFNode yCoordinate = null;

	public HobNobResultSetSolution(QuerySolution solution) {
		this.querySolution = solution;
		setLogger();
	}

	/**
	 * This function provide us to analyze to result set, traverses result set
	 * and initialize class variable with which returned as a solution of a
	 * query.
	 */
	public void analyzeToResultSet() {

		logger.debug("The result set is being analyzed.");
		// traverse result set...

		logger.trace("Results has next.");
		logger.trace("Getting next solution.");
		// getting notification from querySolution...
		isNotification(this.querySolution);
		// getting location from querySolution...
		isLocation(this.querySolution);
		// getting dbpedia from querySolution...
		isDbpedia(this.querySolution);
		// getting population from querySolution...
		isPopulation(this.querySolution);
		// getting population from querySolution...
		isMember(this.querySolution);
		// getting xCoordinate from querySolution...
		isXCoordinate(this.querySolution);
		// getting yCoordinate from querySolution...
		isYCoordinate(this.querySolution);
		// getting type from querySolution...
		isType(this.querySolution);
		// getting label from querySolution...
		isLabel(this.querySolution);
		// getting name from querySolution...
		isName(this.querySolution);
		// getting surname from querySolution...
		isSurname(this.querySolution);

	}

	/**
	 * Getter for dbpedia.
	 * 
	 * @return {@link HobNobResultSetSolution#dbpedia}
	 */
	public RDFNode getDbpedia() {
		return dbpedia;
	}

	/**
	 * Getter for label.
	 * 
	 * @return {@link HobNobResultSetSolution#label}
	 */
	public RDFNode getLabel() {
		return label;
	}

	/**
	 * Getter for location.
	 * 
	 * @return {@link HobNobResultSetSolution#location}
	 */
	public RDFNode getLocation() {
		return location;
	}

	/**
	 * Getter for member.
	 * 
	 * @return {@link HobNobResultSetSolution#member}
	 */
	public RDFNode getMember() {
		return member;
	}

	/**
	 * Getter for name.
	 * 
	 * @return {@link HobNobResultSetSolution#name}
	 */
	public RDFNode getName() {
		return name;
	}

	/**
	 * Getter for notification.
	 * 
	 * @return {@link HobNobResultSetSolution#notification}
	 */
	public RDFNode getNotification() {
		return notification;
	}

	/**
	 * Getter for notificationX
	 * 
	 * @return {@link HobNobResultSetSolution#notificationX}
	 */
	public RDFNode getNotificationX() {
		return notificationX;
	}

	/**
	 * Getter for population.
	 * 
	 * @return {@link HobNobResultSetSolution#population}
	 */
	public RDFNode getPopulation() {
		return population;
	}

	/**
	 * Getter for surname.
	 * 
	 * @return {@link HobNobResultSetSolution#surname}
	 */
	public RDFNode getSurname() {
		return surname;
	}

	/**
	 * Getter for type
	 * 
	 * @return {@link HobNobResultSetSolution#type}
	 */
	public RDFNode getType() {
		return type;
	}

	/**
	 * Getter for xCoordinate
	 * 
	 * @return {@link HobNobResultSetSolution#xCoordinate}
	 */
	public RDFNode getxCoordinate() {
		return xCoordinate;
	}

	/**
	 * Getter for yCoordinate
	 * 
	 * @return {@link HobNobResultSetSolution#yCoordinate}
	 */
	public RDFNode getyCoordinate() {
		return yCoordinate;
	}

	/**
	 * Setter for label. Gets population resource from the solution and sets
	 * {@link HobNobResultSetSolution#label}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	public void setLabel(QuerySolution querySolution) {
		this.label = getRDFNodeFromQuerySolution(querySolution, "label");
	}

	/**
	 * Setter for name. Gets population resource from the solution and sets
	 * {@link HobNobResultSetSolution#name}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	public void setName(QuerySolution querySolution) {
		this.name = getRDFNodeFromQuerySolution(querySolution, "name");
	}

	/**
	 * Setter for notificationX. Gets notification resource from the solution
	 * and sets {@link HobNobResultSetSolution#notificationX}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	public void setNotificationX(QuerySolution querySolution) {
		this.notificationX = getRDFNodeFromQuerySolution(querySolution,
				"notificationX");
	}

	/**
	 * Setter for surname. Gets notification resource from the solution and sets
	 * {@link HobNobResultSetSolution#surname}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	public void setSurname(QuerySolution querySolution) {
		this.surname = getRDFNodeFromQuerySolution(querySolution, "surname");
	}

	/**
	 * Setter for type. Gets type resource from the solution and sets
	 * {@link HobNobResultSetSolution#type}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	public void setType(QuerySolution querySolution) {
		this.type = getRDFNodeFromQuerySolution(querySolution, "type");

	}

	/**
	 * Setter for xCoordinate. Gets xCoordinate from the solution and sets
	 * {@link HobNobResultSetSolution#xCoordinate}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	public void setxCoordinate(QuerySolution querySolution) {
		this.xCoordinate = getRDFNodeFromQuerySolution(querySolution,
				"xCoordinate");
	}

	/**
	 * Setter for yCoordinate. Gets yCoordinate from the solution and sets
	 * {@link HobNobResultSetSolution#yCoordinate}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	public void setyCoordinate(QuerySolution querySolution) {
		this.yCoordinate = getRDFNodeFromQuerySolution(querySolution,
				"yCoordinate");

	}

	/**
	 * This function provide us to get RDFNode which returning as a query
	 * result.
	 * 
	 * @param querySolution
	 *            solution of result set.
	 * @param variableName
	 *            name of the variable which on the SELECT query.
	 * @return gotten node.
	 */
	private RDFNode getRDFNodeFromQuerySolution(QuerySolution querySolution,
			String variableName) {
		// gets the resource which returned by the SELECT
		// query...
		logger.debug(MessageFormat.format("The \"{0}\" node is being gotten",
				variableName));
		RDFNode node = querySolution.get(variableName);

		String ptrn = "RDFNode of the \"{0}\" is \"{1}\".";
		String msg = MessageFormat.format(ptrn, variableName, node);
		logger.info(msg);
		return node;
	}

	/**
	 * Controls dbpedia resource that whether or not in the query solution. İf
	 * it is in the query solution than sets the
	 * {@link HobNobResultSetSolution#dbpedia}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	private void isDbpedia(QuerySolution querySolution) {
		if (querySolution.contains("dbpedia")) {
			setDbpedia(querySolution);
		}
	}

	/**
	 * Controls label resource that whether or not in the query solution. İf it
	 * is in the query solution than sets the
	 * {@link HobNobResultSetSolution#label}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	private void isLabel(QuerySolution querySolution) {
		if (querySolution.contains("label")) {
			setLabel(querySolution);
		}
	}
	
	/**
	 * Controls location resource that whether or not in the query solution. İf
	 * it is in the query solution than sets the
	 * {@link HobNobResultSetSolution#location}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	private void isLocation(QuerySolution querySolution) {
		if (querySolution.contains("location")) {
			setLocation(querySolution);
		}
	}

	/**
	 * Controls member resource that whether or not in the query solution. İf it
	 * is in the query solution than sets the
	 * {@link HobNobResultSetSolution#member}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	private void isMember(QuerySolution querySolution) {
		if (querySolution.contains("member")) {
			setMember(querySolution);
		}
	}

	/**
	 * Controls name literal value that whether or not in the query solution. İf it
	 * is in the query solution than sets the
	 * {@link HobNobResultSetSolution#name}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	private void isName(QuerySolution querySolution) {
		if (querySolution.contains("name")) {
			setName(querySolution);
		}		
	}

	/**
	 * Controls notification resource that whether or not in the query solution.
	 * If it is in the query solution than sets the
	 * {@link HobNobResultSetSolution#notification}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	private void isNotification(QuerySolution querySolution) {
		if (querySolution.contains("notification")) {
			setNotification(querySolution);
		}
		if (querySolution.contains("notificationX")) {
			setNotificationX(querySolution);
		}
	}

	/**
	 * Controls population resource that whether or not in the query solution.
	 * İf it is in the query solution than sets the
	 * {@link HobNobResultSetSolution#population}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	private void isPopulation(QuerySolution querySolution) {
		if (querySolution.contains("population")) {
			setPopulation(querySolution);
		}
	}

	/**
	 * Controls surname literal value that whether or not in the query solution.
	 * İf it is in the query solution than sets the
	 * {@link HobNobResultSetSolution#surname}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	private void isSurname(QuerySolution querySolution) {
		if (querySolution.contains("surname")) {
			setSurname(querySolution);
		}
	}

	/**
	 * Controls type resource that whether or not in the query solution. İf it
	 * is in the query solution than sets the
	 * {@link HobNobResultSetSolution#type}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 * 
	 */
	private void isType(QuerySolution querySolution) {
		if (querySolution.contains("type")) {
			setType(querySolution);
		}
	}

	/**
	 * Controls xCoordinate literal that whether or not in the query solution.
	 * İf it is in the query solution than sets the
	 * {@link HobNobResultSetSolution#xCoordinate}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	private void isXCoordinate(QuerySolution querySolution) {
		if (querySolution.contains("xCoordinate")) {
			setxCoordinate(querySolution);
		}

	}

	/**
	 * Controls yCoordinate literal that whether or not in the query solution.
	 * İf it is in the query solution than sets the
	 * {@link HobNobResultSetSolution#yCoordinate}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	private void isYCoordinate(QuerySolution querySolution) {
		if (querySolution.contains("yCoordinate")) {
			setyCoordinate(querySolution);
		}

	}

	/**
	 * Setter for dbpedia. Gets dbpedia resource from the solution and sets
	 * {@link HobNobResultSetSolution#dbpedia}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	private void setDbpedia(QuerySolution querySolution) {
		this.dbpedia = getRDFNodeFromQuerySolution(querySolution, "dbpedia");
	}

	/**
	 * Setter for location. Gets location resource from the solution and sets
	 * {@link HobNobResultSetSolution#location}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	private void setLocation(QuerySolution querySolution) {
		this.location = getRDFNodeFromQuerySolution(querySolution, "location");
	}

	/**
	 * Creates a logger for this instance with info level.
	 */
	private void setLogger() {
		this.logger = Logger.getLogger(HobNobResultSetSolution.class);
		DOMConfigurator.configure("log4j.xml");
	}

	/**
	 * Setter for member. Gets member resource from the solution and sets
	 * {@link HobNobResultSetSolution#member}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	private void setMember(QuerySolution querySolution) {
		this.member = getRDFNodeFromQuerySolution(querySolution, "member");
	}

	/**
	 * Setter for notification. Gets notification resource from the solution and
	 * sets {@link HobNobResultSetSolution#notification}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	private void setNotification(QuerySolution querySolution) {
		this.notification = getRDFNodeFromQuerySolution(querySolution,
				"notification");
	}

	/**
	 * Setter for population. Gets population resource from the solution and
	 * sets {@link HobNobResultSetSolution#population}
	 * 
	 * @param querySolution
	 *            solution of result set.
	 */
	private void setPopulation(QuerySolution querySolution) {
		this.population = getRDFNodeFromQuerySolution(querySolution,
				"population");
	}
}
